package com.ygqh.baby.utils;

import java.util.Map;
import java.util.Map.Entry;
import java.util.ResourceBundle;

import com.alibaba.fastjson.JSONObject;
import com.foxinmy.weixin4j.util.StringUtil;

import push.PushClient;
import push.android.AndroidBroadcast;
import push.android.AndroidCustomizedcast;
import push.ios.IOSCustomizedcast;

public class UmPushApi {

	private static PushClient client = new PushClient();
	private static String android_appkey = null;
	private static String android_appMasterSecret = null;
	private static String android_appMessageSecret = null;
	private static String ios_appkey = null;
	private static String ios_appMasterSecret = null;
	private static String ios_appMessageSecret = null;
	/**
	 *  可选，正式/测试模式。默认为true
	            测试模式只会将消息发给测试设备。测试设备需要到web上添加。
	    Android: 测试设备属于正式设备的一个子集。
	 */
	private static Boolean production_mode = null;

	static {

		ResourceBundle resourceBundle = ResourceBundle.getBundle("prop/u-push");
		android_appkey = resourceBundle.getString("android.um.appkey");
		android_appMasterSecret = resourceBundle.getString("android.um.master.secret");
		android_appMessageSecret = resourceBundle.getString("android.um.message.secret");

		String productionMode = resourceBundle.getString("um.productionmode");
		if (StringUtil.isNotBlank(productionMode)) {
			production_mode = Boolean.valueOf(productionMode);
		} else {
			production_mode = false;
		}

		ios_appkey = resourceBundle.getString("ios.um.appkey");
		ios_appMasterSecret = resourceBundle.getString("ios.um.master.secret");
		ios_appMessageSecret = resourceBundle.getString("ios.um.message.secret");
	}

	private static String getTimestamp() {
		return Integer.toString((int) (System.currentTimeMillis() / 1000L));
	}

	/**
	 * 安卓app推送消息
	* @Title: sendAndroidAotification 
	*  @param alias 
	*  @param aliasType
	*  @param ticker 通知栏提示文字
	*  @param title 通知标题
	*  @param text 通知文字描述 
	*  @param afterOpen 	  // 点击"通知"的后续行为，
	*  						  "go_app": 打开应用
	                          "go_url": 跳转到URL
	                          "go_activity": 打开特定的activity
	                          "go_custom": 用户自定义内容。
	* @param custom 		 当display_type=message时, 必填
	                       // 当display_type=notification且
	                       // after_open=go_custom时，必填
	                       // 用户自定义内容，可以为字符串或者JSON格式。
	*  @param description  发送消息描述，建议填写。  
	*  @param extra // 可选，JSON格式，用户自定义key-value。只对"通知"
	                       // (display_type=notification)生效。
	                       // 可以配合通知到达后，打开App/URL/Activity使用。
	*  @throws Exception    入参
	* @return void    返回类型
	* @author （guohao） 
	* @throws
	* @date 2018年4月19日 下午4:46:04 
	* @version V1.0
	 * @return 
	 */
	public static boolean sendAndroidAotificationByAlias(String alias, String aliasType, String ticker, String title, String text, String description,
			Map<String, String> extra) {

		try {
			AndroidCustomizedcast customizedcast = new AndroidCustomizedcast(android_appkey, android_appMasterSecret);
			customizedcast.setPredefinedKeyValue("timestamp", getTimestamp());
			customizedcast.setPredefinedKeyValue("alias", alias);
			customizedcast.setPredefinedKeyValue("alias_type", aliasType);

			customizedcast.setPredefinedKeyValue("display_type", "notification");
			customizedcast.setPredefinedKeyValue("ticker", ticker);
			customizedcast.setPredefinedKeyValue("title", title);
			customizedcast.setPredefinedKeyValue("text", text);
			customizedcast.setPredefinedKeyValue("after_open", "go_app");
			customizedcast.setPredefinedKeyValue("description", description);
//			customizedcast.setPredefinedKeyValue("extra", extra);
			if (production_mode) {
				customizedcast.setProductionMode();
			} else {
				customizedcast.setTestMode();
			}
			if (extra != null) {
				for (Entry<String, String> item : extra.entrySet()) {
					customizedcast.setExtraField(item.getKey(), item.getValue());
				}
			}

			return client.send(customizedcast);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public static boolean sendAndroidAotificationByBroadcast(String alias, String aliasType, String ticker, String title, String text, String description,
			Map<String, String> extra) {

		try {
			AndroidBroadcast customizedcast = new AndroidBroadcast(android_appkey, android_appMasterSecret);
			customizedcast.setPredefinedKeyValue("timestamp", getTimestamp());
			customizedcast.setPredefinedKeyValue("alias", alias);
			customizedcast.setPredefinedKeyValue("alias_type", aliasType);

			customizedcast.setPredefinedKeyValue("display_type", "notification");
			customizedcast.setPredefinedKeyValue("ticker", ticker);
			customizedcast.setPredefinedKeyValue("title", title);
			customizedcast.setPredefinedKeyValue("text", text);
			customizedcast.setPredefinedKeyValue("after_open", "go_app");
			customizedcast.setPredefinedKeyValue("description", description);
			if (production_mode) {
				customizedcast.setProductionMode();
			} else {
				customizedcast.setTestMode();
			}
			if (extra != null) {
				for (Entry<String, String> item : extra.entrySet()) {
					customizedcast.setExtraField(item.getKey(), item.getValue());
				}
			}

			return client.send(customizedcast);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 安卓app发送消息
	* @Title: sendAndroidMessage 
	*  @param alias
	*  @param aliasType
	*  @param afterOpen
	*  @param custom 		 当display_type=message时, 必填
	                       // 当display_type=notification且
	                       // after_open=go_custom时，必填
	                       // 用户自定义内容，可以为字符串或者JSON格式。
	*  @param description
	*  @throws Exception    入参
	* @return void    返回类型
	* @author （guohao） 
	* @throws
	* @date 2018年4月19日 下午4:48:35 
	* @version V1.0
	 * @return 
	 */
	public static boolean sendAndroidMessageByAlias(String alias, String aliasType, String afterOpen, String custom, String description) {
		try {
			AndroidCustomizedcast customizedcast = new AndroidCustomizedcast(android_appkey, android_appMasterSecret);
			customizedcast.setAppMasterSecret(android_appMasterSecret);
			customizedcast.setPredefinedKeyValue("appkey", android_appkey);
			customizedcast.setPredefinedKeyValue("timestamp", getTimestamp());
			customizedcast.setPredefinedKeyValue("alias", alias);
			customizedcast.setPredefinedKeyValue("alias_type", aliasType);

			customizedcast.setPredefinedKeyValue("display_type", "message");
			customizedcast.setPredefinedKeyValue("custom", custom);
			customizedcast.setPredefinedKeyValue("after_open", afterOpen);
			customizedcast.setPredefinedKeyValue("description", description);
			if (production_mode) {
				customizedcast.setProductionMode();
			} else {
				customizedcast.setTestMode();
			}
			return client.send(customizedcast);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public static void sendIOSByAlias(String alias, String aliasType, Integer contentAvailable, String title, String subtitle, String body,
			Map<String, String> extra) {
		try {
			IOSCustomizedcast customizedcast = new IOSCustomizedcast(ios_appkey, ios_appMasterSecret);

			customizedcast.setAlias(alias, aliasType);
			customizedcast.setAlert(getIosAlert(title, subtitle, body));
			customizedcast.setBadge(0);
			customizedcast.setSound("default");
			if (production_mode) {
				customizedcast.setProductionMode();
			} else {
				customizedcast.setTestMode();
			}
			if (extra != null) {
				for (Entry<String, String> item : extra.entrySet()) {

					customizedcast.setCustomizedField(item.getKey(), item.getValue());
				}
			}
			client.send(customizedcast);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void sendIOSByAlias(String alias, String aliasType, Integer contentAvailable, String body, Map<String, String> extra) {
		try {
			IOSCustomizedcast customizedcast = new IOSCustomizedcast(ios_appkey, ios_appMasterSecret);

			customizedcast.setAlias(alias, aliasType);
			customizedcast.setAlert(body);
			customizedcast.setBadge(0);
			customizedcast.setSound("default");
			if (production_mode) {
				customizedcast.setProductionMode();
			} else {
				customizedcast.setTestMode();
			}
			if (extra != null) {
				for (Entry<String, String> item : extra.entrySet()) {

					customizedcast.setCustomizedField(item.getKey(), item.getValue());
				}
			}
			client.send(customizedcast);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static JSONObject getIosAlert(String title, String subtitle, String body) {
		JSONObject on = new JSONObject();
		on.put("title", title);
		on.put("subtitle", subtitle);
		on.put("body", body);
		return on;
	}
}
